package ro.imc.training.UMS;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

public class Database {

	/**
	 * @param args
	 */
	private static final String DERBY_DRIVER = "org.apache.derby.jdbc.EmbeddedDriver";
	private static final String DATABASE_URL = "jdbc:derby:UMSDatabase;create=true;";

	
	public Database (){
		
	}
	
	public static void create() throws InstantiationException,
			IllegalAccessException, ClassNotFoundException, SQLException,
			IOException {
		// log.info("Creating database");

		Class.forName(DERBY_DRIVER).newInstance();
		Connection conn = DriverManager.getConnection(DATABASE_URL);

//		conn.createStatement().execute(
//				"create table student(nume varchar(30), prenume varchar(30)) ");
//		conn.createStatement()
//				.execute(
//						"insert into student values ('Grecu', 'Sorin') , ('Rebega' , 'Adrian') ");

		Student s = new Student("a", "a", "a", "a", "a");
//		System.out.println(s.getClass().toString().substring(s.getClass().toString().lastIndexOf(".")+1));
		
//		conn.createStatement()
//		.execute(
//				"insert into student values ('Grecu'," + id + " 7) , ('Rebega' , 'Adrian') ");
		
	
		
		Statement statement = conn.createStatement();
		ResultSet result = statement.executeQuery("select * from student");
		
		ResultSetMetaData rsmd = result.getMetaData();
						
		ResultSet result2 = conn.getMetaData().getTableTypes();
//				getTables(null, null, "student", null);
		 
		while(result2.next()){
			
			System.out.println(result2.getString(1));
			
			
		}
		
		int col = rsmd.getColumnCount();

		for (int i = 1; i <= col; i++)
			System.out.format("%20s", rsmd.getColumnName(i) + " | ");

		while (result.next()) {
			//System.out.println("hih");
//			for (int i = 1; i <= col; i++)
//				System.out.format("%20s", result.getString(i) + " | ");

		}
	}
}
